﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Reflection;

namespace Settings4Net_Demo
{
    public partial class DemoForm : Form
    {
        RamGecTools.Settings4Net MySettings = new RamGecTools.Settings4Net();

        public DemoForm()
        {
            InitializeComponent();
        }
        
        private void button1_Click(object sender, EventArgs e)
        {
            // by the way, Open method return if Setting file managed to load

            if (System.IO.File.Exists("settings.xml") &&
                MySettings.Open("settings.xml", true)) // let's prevent manual changes of the file
                grid.SelectedObject = MySettings.GeneratePropertyGridObject();
            
        }

        private void populateButton_Click(object sender, EventArgs e)
        {
            // since Settings property is a Hashtable you can simply add your settings by assigning them...
            MySettings.Settings["String Value"] = "Look Ma'! I'm using Settings4Net";
            MySettings.Settings["Integer Value"] = 1337;

            // ...or you can use provided functions
            MySettings.Add("Readonly Setting", "You can't change me!", "You need to change readonly attribute before editing");
            MySettings.SetReadonlyProperty("Readonly Setting", true);

            // let's add some different types using different ways
            MySettings.Settings["My Color"] = Color.PeachPuff;
            MySettings.SetItemCategory("My Color", "Colors");
            MySettings.Add<Color>("Color2", Color.FromArgb(128, 64, 198), "Description od Color2", "Colors", "Extra stuff that I can add here to identify my setting");
            MySettings.Add("Color3", Color.Gainsboro, "Color3 description goes here", "Colors", null);

            MySettings.Settings["Double Value"] = 3.1415;
            MySettings.Settings["Save Font"] = SystemFonts.DefaultFont;
            MySettings.Settings["Date and Time"] = new DateTime(1985, 8, 20, 2, 55, 0);

            MySettings.Settings["Hidden"] = "You won't see this in your PropertyGrid";
            MySettings.SetItemVisibility("Hidden", false);

            // finally let's show it in our PropertyGrid
            grid.SelectedObject = MySettings.GeneratePropertyGridObject();
        }

        private void saveButton_Click(object sender, EventArgs e)
        {
            // simple huh?
            MySettings.Save("settings.xml");
        }

        private void testButton_Click(object sender, EventArgs e)
        {
            RamGecTools.Settings4Net SettingsExample = new RamGecTools.Settings4Net();
            SettingsExample.Open("example.xml");

            testLabel.Text = SettingsExample.GetItemOrDefaultValue("LabelText", "Text loaded from settings").ToString();
            testLabel.Font = (Font)SettingsExample.GetItemOrDefaultValue("LabelFont", SystemFonts.CaptionFont);
            testLabel.ForeColor = (Color)SettingsExample.GetItemOrDefaultValue("LabelColor", Color.Red);

            testTextBox.Text = SettingsExample.GetItemOrDefaultValue("BoxText", "This text is also dynamically loaded from settings. \r\n http://settings4net.codeplex.com").ToString();
            testTextBox.Font = (Font)SettingsExample.GetItemOrDefaultValue("BoxFont", SystemFonts.IconTitleFont);
            testTextBox.BackColor = (Color)SettingsExample.GetItemOrDefaultValue("BoxColor", Color.Thistle);

            testButton.Text = SettingsExample.GetItemOrDefaultValue("ButtonText", "Check the source code for more details").ToString();
            
        }
    }
}
